<?
class iGrid extends AppLog
{
	var $query;				// query creata
	var $border;			// visuaizza bordo
	var $form;				// nome form che contiene la griglia
	var $adodb;				// classe adodb
	var $rs;				// connesione adodb
	var $grid_name;			// nome griglia
	var $grid_style;		// classe stile griglia
	var $grid_width;		// larghezza griglia
	var $grid_height;		// altezza griglia
	var $title;				// titolo griglia se presente viene visualizzato
	var $title_align;		// allignamento titolo griglia
	var $title_style;		// stile titolo griglia
	var $title_height;		// altezza colonna in percentuale

//-----------------------------------------------------------------------------------HEADER
	var $head=1;			// intestazione colonne invisibile=0 visibile=1
	var $head_style;		// lo stile dell'intestazione delle colonne
	var $head_type="HREF";	// BUTTON disegno pulsante x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// HREF   link di riferimento x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// NONE	  disabilita anche l'ordinamento
	var $head_height=10;	// altezza intestazione
	var $head_align;		// alignamento nome campi intestazione left center right
//----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------TOOLBAR
	var $toolbar = 0;		// 0 o 1 fondo della griglia
	var $toolbar_style;		// stile fondo della griglia
	var $toolbar_height;	// altezza fondo della griglia
	var $toolbar_image;		// immagine fondo della griglia

//-----------------------------------------------------------------------------------FOOTER
	var $foot=0;			// 0 o 1 fondo della griglia
	var $foot_style;		// stile fondo della griglia
	var $foot_height;		// altezza fondo della griglia
	var $foot_image;		// immagine fondo della griglia

	var $curr_page=1;		// pagine attualmente visualizzata
	var $pages=0;			// pagine totali
	var $page_up_style;		// stile pulsante pagina avanti
	var $page_down_style;	// stile pulsante pagina avanti
	var $text_style;		// stile testo pagine correnti
	var $orderby="";		// posizione campo dell'ordinamento

// Inserimento di testo,pulsanti o link nell footer della griglia
	var $foot_type;			// BUTTON disegno pulsante x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// HREF   link di riferimento x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
	var $foot_name;			// nome
	var $foot_sty;			// stile
	var $foot_onclick;		// azione javascript in caso di selezione da pulsante o riferimento
	var $foot_value;		// valore da inserire nell'herf
	var $foot_label;		// testo da visualizzare
	var $foot_num=0;		// numero oggetti
//----------------------------------------------------------------------------------------------

// Inserimento di testo,pulsanti o link nell header della griglia
	var $toolbar_type;		// BUTTON disegno pulsante x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// HREF   link di riferimento x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
	var $toolbar_name;		// nome
	var $toolbar_sty;		// stile
	var $toolbar_onclick;	// azione javascript in caso di selezione da pulsante o riferimento
	var $toolbar_value;		// valore da inserire nell'herf
	var $toolbar_label;		// testo da visualizzare
	var $toolbar_num=0;		// numero oggetti
	var $toolbar_colspan;
//----------------------------------------------------------------------------------------------

	var $row_num;			// numero righe da visualizzare
	var $row_clear = true;	// disegna righe vuote se il records non completano la griglia
							// true = disegna
							// false = no disegna
	var $row_style;			// stile righe
	var $row_onclick;		// azione nel caso della selezione della riga
	var $row_onclick_if;	// condizione per la scelta dell'azione sulla riga
	var $row_height;		// altezza della riga
	var $row_type;			// NORMAL viene mostrato solo un tipo di stile per le righe
							// ALTERNATE viene mostrato piu di un tipo di stile per le righe
	var $pos_row=0;			// contatore posizione righe
	var $pos_row_onclick=0;	// contatore 

	var $col_name;			// nome colonna
	var $col_style;			// stile colonna
	var $col_nohead;		// no stampa il titolo della colonna
	var $col_label;			// testo da visualizzare nella colonna
	var $col_onclick;		// azione javascript in caso di selezione da pulsante o riferimento
	var $col_select;		// utilizzata per creare la select
	var $col_format;		// stabilisce il formato di visualizzazione
	var $col_image;			// un immagine da inserire nella colonna
	var	$col_img_width;		// larghezza immagine
	var $col_img_height;	// altezza immagine
	var $col_visible;		// 0 o 1 se la colonna e visibile assiociato con la select per 
	var $col_index;
	var $col_width;			// larghezza colonna in percentuale
	var $col_height;		// altezza colonna in percentuale
	var $col_adodb;			// connessione al database colonna
	var $col_rs;				// resultset colonna
	var $col_subquery;		// query alternativa a quella principale della visualizzazione
	var $col_if_res;		// risultato controllo condizione
	var $col_if;			// controllo condizione se verificata verra eseguito cio che indicato nelle variabili
							// di seguito
	var $col_div;			// divisione contenuto cella su piu colonne
	var $col_div_separatore;// separatore divisione contenuto cella
	var $col_if_style;		// azione sullo style
	var $col_if_value;		// azione sul valore
	var $col_type;			// BUTTON disegno pulsante x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
							// HREF   link di riferimento x selezione record
							//		  possono essere presenti piu pulsanti in diverse colonne
							//		  e sufficiente specificare col_onclick per l'azione
	var $col_style_link;	// stile button
	var $col_align;			// alignamento colonna orizzontale left center right
	var $col_valign;		// alignamento colonna verticale baseline bottom middle top
	var $col_pos;			// posizione colonna
	var $col_value = array();
	var $num_col=0;			// posizione colonna visibili
	var $pos_col=0;			// contatore posizione colonna

	var $font_size;			// dimensione font
	var $from;
	var $where;
	var $path_images;

	var $err;
	var $a;
	var $kname;
	var $name;
	var $tkey;
	var $tval;
	var $fkey;
	var $fval;
	var $tstr;
	var $tnum;
    var $row_cnt;
	var $tr_cnt;
	var $tr_tmp;
	var $teval;

	var $request = array();
	var $session = array();
	var $outHtml = '';
	
	function iGrid() 
	{
		$this->row_style=array();
		$this->col_name=array();
		$this->col_style=array();
		$this->col_onclick=array();
		$this->col_orderby=array();
		$this->col_select=array();
		$this->col_format=array();
		$this->col_image=array();
		$this->col_visible=array();
		$this->col_width=array();
		$this->col_height=array();
		$this->col_adodb=array();
		$this->col_rs=array();
		$this->col_subquery=array();
		$this->col_if=array();
		$this->col_if_res=array();
		$this->col_if_value=array();
		$this->col_if_style=array();
		$this->from=array();
		$this->where=array();

		$this->row_style=array();
		$this->row_onclick=array();
		$this->row_height=array();

		$this->foot_name=array();
		$this->foot_style=array();
		$this->foot_onclick=array();
		$this->foot_label=array();
		$this->foot_value=array();
	}
	
	function rows_select($onclick="",$if="") {
		$this->row_onclick[$this->pos_row_onclick]=$onclick;
		$this->row_onclick_if[$this->pos_row_onclick]=$if;
		$this->pos_row_onclick++;
	}
	
	function rows($style="",$height=0) {
		$this->row_style[$this->pos_row]=$style;
		$this->row_height[$this->pos_row]=$height;
		$this->pos_row++;
	}

	function select($field,$name,$style="",$onclick="",$format="",$image="",$img_w=0,$img_h=0,$visible=1,$width=0,$height=0,$type="",$style_btn="") {
		$this->col_select[$name]=$field;
		$this->col_name[$name]=$name;
		$this->col_pos[$name]=$this->pos_col;
		$this->col_style[$name]=$style;
		$this->col_onclick[$name]=$onclick;
		$this->col_format[$name]=$format;
		$this->col_image[$name]=$image;
		$this->col_img_width[$name]=$img_w;
		$this->col_img_height[$name]=$img_h;
		$this->col_visible[$name]=$visible;
		$this->col_width[$name]=$width;
		$this->col_height[$name]=$height;
		$this->col_type[$name]=$type;
		$this->col_style_link[$name]=$style_btn;
		$this->pos_col++;
		if ($visible==1) $this->num_col++;
	}

	function select_add( &$select ) 
	{
		$name						= $select["name"];
		
		$this->col_select[$name]	= $select["field"];									// campo database
		
		// nome colonna
		$this->col_name[$this->pos_col]		= $select['name'];
		
		$this->col_nohead[$name]	= (isset($select['nohead'])) ? $select['nohead'] : 0;	// nome griglia
		$this->col_label[$name]		= (isset($select["label"])) ? $select["label"] : "";	// nome griglia
		
		$this->col_pos[$name]		= $this->pos_col;									// posizione griglia
		$this->col_style[$name]		= $select["style"];									// style colonna
		$this->col_onclick[$name]	= $select["onclick"];								// funzione onclick
		
		if (isset($select["format"])) $this->col_format[$name]=$select["format"];		// formato di visualizzazione
			
		$this->col_image[$name]		= $select["image"];									// immagine da vis. in colonna
		$this->col_img_width[$name]	= $select["img_w"];									// larghezza imagine
		$this->col_img_height[$name]= $select["img_h"];									// altezza immagine
		
		$this->col_width[$name]		= (isset($select['width']))? $select['width'] : '' ;	// larghezza colonna
		$this->col_height[$name]	= (isset($select["height"]))? $select["height"] : '' ;	// altezza colonna
		
		$this->col_type[$name]		= $select["type"];			// tipo NONE, BUTTON, HREF
		$this->col_adodb[$name]		= $select["adodb"];	// subquery per valore differente dalla quaery princiaple
		$this->col_subquery[$name]	= $select["subquery"];	// subquery per valore differente dalla quaery princiaple
		$this->col_style_link[$name]= $select["stybtn"];	// stile button

		if (isset($select["align"])) $this->col_align[$name]=$select["align"];	// alliniamento orizzontale
		if (isset($select["valign"])) $this->col_valign[$name]=$select["valign"];	// alliniamento veritcale

		$this->col_if[$name]		= $select["if"];
		$this->col_if_res[$name]	= "";
		$this->col_if_value[$name]	= $select["if_value"];
		$this->col_if_style[$name]	= $select["if_style"];
		
		$this->col_div[$name]				= (isset($select['divColonne']))? $select['divColonne'] : -1 ;
		$this->col_div_separatore[ $name ] 	= $select['divSperatore'];
		unset($select['divColonne'], $select['divSperatore']);
		
		$this->pos_col++;
		
		if ( isset( $select['visible'] ) ) 
		{
			if ( $select['visible'] == 0 ) 
			{
				$this->col_visible[ $name ]	= 0;
				unset( $select['visible'] );
				return;
			}
		}
		$this->col_visible[ $name ]	= 1;								// colonna visibile
		$this->num_col++;
	}

	function toolbar_add( &$select ) 
	{
		$name						= $select["toolbar_name"];
		$this->toolbar_num++;
		$this->toolbar_type[$name]	= $select["toolbar_type"];
		$this->toolbar_name[$name]	= $select["toolbar_name"];
		$this->toolbar_sty[$name]	= $select["toolbar_style"];
		$this->toolbar_onclick[$name]= $select["toolbar_onclick"];
		$this->toolbar_value[$name]	= $select["v_value"];
		$this->toolbar_label[$name]	= (isset($select["toolbar_label"])) ? $select["toolbar_label"] : '';	
		$this->toolbar_colspan[$name]	= (isset($select["toolbar_colspan"])) ? $select["toolbar_colspan"] : 1;
	}
	
	function foot_add(&$select) 
	{
		$name=$select["foot_name"];
		$this->foot_num++;
		$this->foot_type[$name]=$select["foot_type"];
		$this->foot_name[$name]=$select["foot_name"];
		$this->foot_sty[$name]=$select["foot_style"];
		$this->foot_onclick[$name]=$select["foot_onclick"];
		$this->foot_value[$name]=$select["foot_value"];
		$this->foot_label[$name]=(isset($select["foot_label"])) ? $select["foot_label"] : "";	
	}

	function orderby($field,$name) 
	{
		$this->col_orderby[$name]=$field;
	}

	function view_query() 
	{
		print $this->query."<BR>";
	}

	function make_query() 
	{
		$this->query="SELECT DISTINCT ";
		$this->a=0;
		foreach($this->col_select as $this->name => $this->field) 
		{
			if ( $this->a == 1) 
			{
				$this->query .= ','; 
			} 
			else 
			{
				$this->a = 1;
			}
			
			if ($this->field != '') 
			{
				$this->query .= $this->field." AS '".$this->name."' ";
			} 
			else 
			{
				$this->query .= "'' AS '".$this->name."' ";
			}
		}
		$this->query .= 'FROM '.$this->from.' ';
		if ($this->where != '') $this->query .= "WHERE ".$this->where." ";
/*
		if ($this->orderby!="") {
			settype($this->orderby,"integer");
			if ($this->orderby < 0) {
				$this->query .= "ORDER BY ".($this->orderby * -1)." DESC";
			} else {
				$this->query .= "ORDER BY ".$this->orderby." ASC";
			}
		}
*/
		if ($this->orderby!="") 
		{
			$this->query .= "ORDER BY ".$this->orderby;
		}
		//print $this->query."<br>";
		$this->logWrite('Query: '.$this->query,9);
	}
/*
------------------------------------------------------------------------------------------------------------
	DISEGNA GRIGLIA
------------------------------------------------------------------------------------------------------------
*/

	function show() 
	{
		foreach($this->reuqest as $this->tkey => $this->tval) 
		{
			if ($this->tkey==$this->grid_name."_curr_page") $this->curr_page=$this->tval;
			if ($this->tkey==$this->grid_name."_pages") $this->pages=$this->tval;
			if ($this->tkey==$this->grid_name."_orderby") $this->orderby=$this->tval;
		}

		if ($this->curr_page < 1) $this->curr_page = 1;
		if ($this->curr_page > $this->pages && $this->pages > 0) $this->curr_page = $this->pages;

		$this->make_query();

		if ($this->row_num > 0) 
		{
			$this->rs = $this->adodb->PageExecute($this->query,$this->row_num,$this->curr_page);
			if (!$this->rs) 
			{
		  		$this->err=$this->adodb->ErrorMsg();
				return "ERR";
			}
			$this->pages = $this->rs->LastPageNo();
		} 
		else 
		{
			$this->rs = $this->adodb->Execute($this->query);
			if ( ! $this->rs ) 
			{
		  		$this->err=$this->adodb->ErrorMsg();
				return "ERR";
			}
		}

		$this->outHtml.=  '<TABLE name="'.$this->grid_name.'" id="'.$this->grid_name.'" ';
		
		if ($this->grid_style != '') $this->outHtml.=  "class=\"".$this->grid_style."\" ";
		
		if ($this->border == 1) $this->outHtml.=  'border="1" ';
		
		if ($this->grid_height != '' ) $this->outHtml.=  'height="'.$this->grid_height.'px" ';
		
		if ($this->grid_width != '' ) $this->outHtml.=  'width="'.$this->grid_width.'px" ';
		
		$this->outHtml.= ">\n";

/*
------------------------------------------------------------------------------------------------------------
	Titolo
------------------------------------------------------------------------------------------------------------
*/
	$this->outHtml.=  "<thead>\n";
	if ($this->title != '') 
	{
		$this->outHtml.=  "<TR ";
		if ($this->title_style!="") 
			$this->outHtml.= "class=\"".$this->title_style."\" ";
		
		if ($this->title_height>0) 
			$this->outHtml.=  "height=\"".$this->ris_y($this->title_height)."\" ";
		$this->outHtml.=  ">\n";
		
		$this->outHtml.=  "<TD ";
		if ($this->title_align!="") 
			$this->outHtml.=  "align=\"".$this->title_align."\" ";
		$this->outHtml.=  "colspan=\"".$this->num_col."\">".$this->title."</TD>\n";
		$this->outHtml.=  "</TR>\n";
	}

/*
------------------------------------------------------------------------------------------------------------
	Toolbar
------------------------------------------------------------------------------------------------------------
*/
	if ( $this->toolbar == 1 ) 
	{
		$this->outHtml.=  "<tr ";
		if ( $this->toolbar_style != '' ) 
			$this->outHtml.=  "class=\"".$this->toolbar_style."\" ";
		
		if ( $this->toolbar_height != '' ) 
			$this->outHtml.=  "height=\"".$this->toolbar_height."\" ";
		
		$this->outHtml.=  ">\n";
		
		$this->tnum = $this->num_col + 1;
		$tmp = '';
		
		foreach($this->toolbar_name as $this->kname => $this->name) 
		{
			//print $this->name."<br>";
			if ( $this->toolbar_colspan[ $this->name ] != '' )
			{
				$this->tnum -= $this->toolbar_colspan[ $this->name ];
			}

			switch ( $this->toolbar_type[ $this->name ] ) 
			{
				case 'BUTTON' :
					$tmp.=  "<TD>\n";
					
					$tmp.=  "<INPUT type=\"BUTTON\" ";
					if ($this->toolbar_sty[$this->name]!="") $tmp.=  "class=\"".$this->toolbar_sty[$this->name]."\" ";
					if ($this->toolbar_onclick[$this->name]!="") $tmp.=  "onclick=\"".$this->toolbar_onclick[$this->name]."\" ";
					if ($this->toolbar_label[$this->name]!="") $tmp.=  "value=\"".$this->toolbar_label[$this->name]."\" ";
					$tmp.=  ">";
					break;
					
				case 'HREF' :
					$tmp.=  "<TD>\n";
					$tmp.=  "<A href=\"".$this->toolbar_value[$this->name]."\" ";
					if ($this->toolbar_sty[$this->name]!="") $tmp.=  "class=\"".$this->toolbar_sty[$this->name]."\" ";
					if ($this->toolbar_onclick[$this->name]!="") $tmp.=  "onclick=\"".$this->toolbar_onclick[$this->name]."\" ";
					$tmp.=  ">".$this->toolbar_label[$this->name]."</A>";
					break;
					
				case 'TD' :
					$tmp.=  "<td";
					if ( $this->toolbar_sty[$this->name] != '' ) 
						$tmp.=  " class=\"".$this->toolbar_sty[ $this->name ]."\" ";
					
					if ( $this->toolbar_onclick[$this->name] != '') 
						$tmp.=  " onClick=\"".$this->toolbar_onclick[ $this->name ]."\" ";

					if ( $this->toolbar_colspan[$this->name] != '') 
						$tmp.=  " colspan=\"".$this->toolbar_colspan[ $this->name ]."\" ";

					$tmp.=  ">\n";
					$tmp.=  $this->toolbar_label[ $this->name ];
					break;
					
				default :
					$tmp.=  $this->toolbar_label[$this->name];
			}
			$tmp.=  "</td>\n";
		}
		
		$this->outHtml.=  "<td colspan=\"".$this->tnum."\">&nbsp;</td>\n";
		
		$this->outHtml.=  $tmp;
		
		$this->outHtml.=  "</tr>\n";
	}

/*
------------------------------------------------------------------------------------------------------------
	Nome Colonne Intestazione
------------------------------------------------------------------------------------------------------------
*/
	if ($this->head == 1) 
	{
		$this->outHtml.=  "<TR ";
		if ($this->head_style!="") 
			$this->outHtml.=  "class=\"".$this->head_style."\" ";
		
		if ($this->head_height>0) 
			$this->outHtml.=  "height=\"".$this->ris_y($this->head_height)."\" ";
		
		$this->outHtml.=  ">\n";	
		foreach($this->col_name as $this->kname => $this->name) 
		{
			if ( $this->col_visible[$this->name] == 1 ) 
			{
				$this->outHtml.=  "<TD ";
				
				if ($this->head_align!="") 
					$this->outHtml.=  "align=\"".$this->head_align."\" ";
				
				$this->outHtml.=  ">";
				
				if ( ! $this->col_nohead[ $this->name ] )
				{
					if ($this->rs->EOF) 
					{
						$this->outHtml.=  $this->name;
					} 
					else 
					{
						switch ($this->head_type) 
						{
							case 'BUTTON' :
								$this->hbutton($this->name,$this->col_pos[$this->name] + 1);
								break;
							case 'HREF' :
								$this->hhref($this->name,$this->col_pos[$this->name] + 1);
								break;
							case 'NONE' :
								$this->outHtml.=  $this->name;
						}
					}
				}
				$this->outHtml.=  "</TD>\n";
			}
		}
		$this->outHtml.=  "</TR>\n";
	}
	
	$this->outHtml.=  "</thead>\n";

		$this->tr_cnt=0;
        $this->row_cnt=0;
		$this->tr_tmp=count($this->row_style) - 1;

		while (!$this->rs->EOF) {
/*
------------------------------------------------------------------------------------------------------------
	Disegna Righe
------------------------------------------------------------------------------------------------------------
*/
			$this->outHtml.=  "<TR ";
			if ($this->tr_tmp>0) {
				if ($this->row_type=="ALTERNATE") {
					if ($this->tr_cnt > $this->tr_tmp) $this->tr_cnt=0;
					$this->outHtml.=  "class=\"".$this->row_style[$this->tr_cnt]."\" ";
					$this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
					$this->tr_cnt++;
				} else {
					if ($this->row_style[0]!="") $this->outHtml.=  "class=\"".$this->row_style[0]."\" ";
					if ($this->row_height[0]!=0) $this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
				}
				
				if ($this->pos_row_onclick>0) {
					foreach($this->row_onclick as $this->tkey => $this->tval) {
						if ($this->row_onclick_if[$this->tkey]=="") {
							$this->find_field($this->rs,$this->tval);
							$this->outHtml.=  "onclick=\"".$this->tval."\" ";
						}
					}
				}
			}
			$this->outHtml.=  ">\n";

/*
------------------------------------------------------------------------------------------------------------
	Disegna Colonne
------------------------------------------------------------------------------------------------------------
*/
			foreach($this->col_name as $this->kname => $this->name) 
			{
				//print $this->kname.' - '.$this->name."<br>";
				if ($this->col_visible[$this->name]==1) 
				{
					$this->outHtml.=  "<TD ";

/*
------------------------------------------------------------------------------------------------------------
	Crea Subquery
------------------------------------------------------------------------------------------------------------
*/
					if ($this->col_subquery[$this->name] != '') 
					{
						$this->tstr = $this->col_subquery[$this->name];
						$this->find_field($this->rs,$this->tstr);
//print $this->tstr;
//exit;
						$this->col_rs[$this->name] = $this->col_adodb[$this->name]->Execute($this->tstr);
						if (!$this->col_rs[$this->name]) 
						{
							$this->err=$this->col_adodb[$this->name]->ErrorMsg();
							return "ERR";
						}
						$this->col_value[$this->name] = $this->col_rs[$this->name]->fields[0];
					}
//------------------------------------------------------------------------------------------------------------
					if ($this->col_if[$this->name]!="") 
					{
						if (!preg_match("/&/",$this->col_if[$this->name])) 
						{
							if ($this->col_subquery[$this->name]=='') 
							{
								$this->teval="if ( \$this->rs->fields[\$this->col_pos[\$this->name]] ".$this->col_if[$this->name]." ) {\n";
							} 
							else 
							{
								$this->teval="if ( \$this->col_rs[\$this->name]->fields[0] ".$this->col_if[$this->name]." ) {\n";
							}
						} 
						else 
						{
							$tmp_if = split("&",$this->col_if[$this->name]);
							$this->col_if[$this->name]=str_replace("sub","",$this->col_if[$this->name]);
							$tmp_val_1 = ($this->col_value[$tmp_if[1]] != "")? $this->col_value[$tmp_if[1]] : 0;
							$tmp_val_2 = ($this->col_rs[$this->name]->fields[0] != "")? $this->col_rs[$this->name]->fields[0] : 0;
							$this->teval="if ( ".$tmp_val_1." ".$tmp_if[0]." ".$tmp_val_2." ) {\n";
						}
						$this->teval.= "\$this->col_if_res[\$this->name] = ";
						$this->teval.= ($this->col_if_style[$this->name]!="") ? "\"OK\"" : "\"\"" ;
						$this->teval.= ";\n" ;
						$this->teval.="} else {\n";
						$this->teval.= "\$this->col_if_res[\$this->name] = '';\n";
						$this->teval.="}";
//print "<br>";
//print $this->teval;
//exit;
						eval($this->teval);
					}

					if ($this->col_align[$this->name]!="") $this->outHtml.=  "align=\"".$this->col_align[$this->name]."\" ";
					if ($this->col_valign[$this->name]!="") $this->outHtml.=  "valign=\"".$this->col_valign[$this->name]."\" ";
					
					if ($this->col_if_res[$this->name]!="") 
					{
						$this->outHtml.=  "class=\"".$this->col_if_style[$this->name]."\" ";
					} 
					elseif ($this->col_style[$this->name]!="") 
					{
						$this->outHtml.=  "class=\"".$this->col_style[$this->name]."\" ";
					}
					
					if ($this->col_width[ $this->name ] != '') 
						$this->outHtml.=  'width="'.$this->col_width[$this->name].'" ';
					
					if ($this->col_height[ $this->name ] != '') 
						$this->outHtml.=  'height="'.$this->col_height[$this->name].'" ';

					switch ($this->col_type[$this->name]) 
					{
/*
------------------------------------------------------------------------------------------------------------
	TD Pulsante
------------------------------------------------------------------------------------------------------------
*/
						case 'TD' :
							$this->outHtml.=  "onClick='".$this->searchVars( $this->rs, $this->col_onclick[ $this->name ] )."' >\n";
							$this->outHtml.= $this->col_label[ $this->name ];
							break;
/*
------------------------------------------------------------------------------------------------------------
	Disegna Pulsante
------------------------------------------------------------------------------------------------------------
*/
		                case 'BUTTON' :
							$this->outHtml.=  ">\n";
							
							if ($this->col_subquery[$this->name]=="") 
							{
								$this->button($this->rs,$this->name);
							} 
							else 
							{
								$this->button($this->col_rs[$this->name],$this->name);
							}
							break;
/*
------------------------------------------------------------------------------------------------------------
	Disegna Link
------------------------------------------------------------------------------------------------------------
*/
		                case 'HREF' :
							$this->outHtml.=  ">\n";
							
							if ($this->col_subquery[$this->name]=="") 
							{
								$this->href($this->rs,$this->name);
							} 
							else 
							{
								$this->button($this->col_rs[$this->name],$this->name);
							}
                            break;

                    	default :
							$this->outHtml.=  ">\n";
							
							if ($this->col_image[$this->name]=="") 
							{
								if ($this->col_subquery[$this->name]=="") 
								{
									$this->outHtml.=  $this->format($this->rs->fields[$this->col_pos[$this->name]],$this->name);
								} 
								else 
								{
									$cnt = 0;
									$tmp = '';
									//$wid = intval(100 / $this->col_div[ $this->name ]);
									while(!$this->col_rs[$this->name]->EOF) 
									{
										if ( $cnt >= $this->col_div[ $this->name ] && $this->col_div[ $this->name ] != -1 )
										{
											$tmp .= "<br />\n";
											$cnt = 0;
										}
										elseif ( $tmp != '' && $this->col_div[ $this->name ] != -1 )
										{
											$tmp .= $this->col_div_separatore[ $this->name ];
										}
										$cnt++;
										$tmp.= $this->format( $this->col_rs[$this->name]->fields[0], $this->name );
										$this->col_rs[$this->name]->MoveNext();
									}
									$tmp.= '';
									$this->outHtml.= $tmp;
								}
							} 
							else 
							{
								$this->outHtml.=  "<IMG src=\"".$this->path_images."/".$this->col_image[$this->name]."\" width=\"".$this->ris_x($this->col_img_width[$this->name])."\" height=\"".$this->ris_y($this->col_img_height[$this->name])."\">";
							}
                    } 
					$this->outHtml.=  "</TD>\n";
				}
			}
			$this->outHtml.=  "</TR>\n";
            $this->row_cnt++;
			$this->rs->MoveNext();
		}

/*
------------------------------------------------------------------------------------------------------------
    Disegna righe vuote se i record non riescoa completare la pagina
------------------------------------------------------------------------------------------------------------
*/
		if ( $this->row_clear )
		{
			while($this->row_cnt < $this->row_num) 
			{
				$this->outHtml.=  "<TR ";
				if ($this->tr_tmp>0) 
				{
					if ($this->row_type=="ALTERNATE") 
					{
						if ($this->tr_cnt > $this->tr_tmp) $this->tr_cnt=0;
						$this->outHtml.=  "class=\"".$this->row_style[$this->tr_cnt]."\" ";
						$this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
						$this->tr_cnt++;
					} 
					else 
					{
						if ($this->row_style[0]!="") $this->outHtml.=  "class=\"".$this->row_style[0]."\" ";
						if ($this->row_height[0]!=0) $this->outHtml.=  "height=\"".$this->ris_y($this->row_height[0])."\" ";
					}
					
					if ($this->pos_row_onclick>0) 
					{
						foreach($this->row_onclick as $this->tkey => $this->tval) 
						{
							if ($this->row_onclick_if[$this->tkey]=="") 
							{
								$this->find_field($this->rs,$this->tval);
								$this->outHtml.=  "onclick=\"".$this->tval."\" ";
							}
						}
					}
				}
				$this->outHtml.=  ">\n";
				foreach($this->col_name as $this->kname => $this->name) 
				{
					if ($this->col_visible[$this->name]==1) 
					{
						$this->outHtml.=  "<TD ";
						
						if ($this->col_width[ $this->name ] != '') 
							$this->outHtml.=  'width="'.$this->col_width[$this->name].'" ';
						
						if ($this->col_height[ $this->name ] != '') 
							$this->outHtml.=  'height="'.$this->col_height[$this->name].'" ';
						
						$this->outHtml.=  "></TD>\n";
					}
				}
				$this->outHtml.=  "</TR>\n";
				$this->row_cnt++;
			}
		}
/*
------------------------------------------------------------------------------------------------------------
	Disegna Footer
------------------------------------------------------------------------------------------------------------
*/
		if ( $this->foot == 1 ) 
		{
			$this->outHtml.=  "<tfoot>\n";
			$this->outHtml.=  "<TR ";
			if ( $this->foot_style != '' ) 
				$this->outHtml.=  "class=\"".$this->foot_style."\" ";
			
			if ( $this->foot_height > 0 ) 
				$this->outHtml.=  "height=\"".$this->ris_y($this->foot_height)."\" ";
			
			$this->outHtml.=  ">\n";
			
			if ($this->foot_num > $this->num_col) 
			{
				$this->tnum = $this->foot_num - 1;
			} 
			elseif ($this->foot_num < $this->num_col) 
			{
				$this->tnum = $this->num_col - $this->foot_num;
			} 
			
			$this->outHtml.=  "<TD colspan=\"".$this->tnum."\">\n";
			
			if ( $this->pages > 1 ) 
			{
				if ( $this->curr_page - 10 > 1) 
				{
					$this->outHtml.=  "<INPUT type=\"button\" onclick=\"".$this->grid_name."_page_down(10)\" ";
					if ($this->page_down_style!="") $this->outHtml.=  "class=\"".$this->page_down_style."\" ";
					$this->outHtml.=  "value=\"10 Pag.Indietro\">&nbsp;";
				}
				if ( $this->curr_page > 1 )
				{
					$this->outHtml.=  "<INPUT type=\"button\" onclick=\"".$this->grid_name."_page_down(1)\" ";
					if ($this->page_down_style!="") $this->outHtml.=  "class=\"".$this->page_down_style."\" ";
					$this->outHtml.=  "value=\"Pag.Indietro\">&nbsp;";
				}
				$this->outHtml.=  "<LABEL class=\"".$this->text_style."\">".$this->curr_page."/".$this->pages."</LABEL>";
				if ( $this->curr_page < $this->pages ) 
				{
					$this->outHtml.=  "&nbsp;<INPUT type=\"button\" onclick=\"".$this->grid_name."_page_up(1)\" ";
					if ($this->page_up_style!="") $this->outHtml.=  "class=\"".$this->page_up_style."\" ";
					$this->outHtml.=  "value=\"Pag.Avanti\">&nbsp;";
				}
				if ($this->curr_page + 10 < $this->pages) 
				{
					$this->outHtml.=  "<INPUT type=\"button\" onclick=\"".$this->grid_name."_page_up(10)\" ";
					if ($this->page_up_style!="") $this->outHtml.=  "class=\"".$this->page_up_style."\" ";
					$this->outHtml.=  "value=\"10 Pag.Avanti\">";
				}
			} 
			else 
			{
				$this->outHtml.=  "&nbsp;";
			}
			$this->outHtml.=  "</TD>\n";

			foreach($this->foot_name as $this->kname => $this->name) 
			{
				switch ($this->foot_type[$this->name]) 
				{
	                case 'BUTTON' :
						$this->outHtml.=  "<TD>\n";
						
						$this->outHtml.=  "<INPUT type=\"BUTTON\" ";
						if ($this->foot_sty[$this->name]!="") $this->outHtml.=  "class=\"".$this->foot_sty[$this->name]."\" ";
						if ($this->foot_onclick[$this->name]!="") $this->outHtml.=  "onclick=\"".$this->foot_onclick[$this->name]."\" ";
						if ($this->foot_label[$this->name]!="") $this->outHtml.=  "value=\"".$this->foot_label[$this->name]."\" ";
						$this->outHtml.=  ">";
						break;
						
	                case 'HREF' :
						$this->outHtml.=  "<TD>\n";
						$this->outHtml.=  "<A href=\"".$this->foot_value[$this->name]."\" ";
						if ($this->foot_sty[$this->name]!="") $this->outHtml.=  "class=\"".$this->foot_sty[$this->name]."\" ";
						if ($this->foot_onclick[$this->name]!="") $this->outHtml.=  "onclick=\"".$this->foot_onclick[$this->name]."\" ";
						$this->outHtml.=  ">".$this->foot_label[$this->name]."</A>";
						break;
						
	                case 'TD' :
						$this->outHtml.=  "<td";
						if ( $this->foot_sty[$this->name] != '' ) 
							$this->outHtml.=  " class=\"".$this->foot_sty[ $this->name ]."\" ";
						
						if ( $this->foot_onclick[$this->name] != '') 
							$this->outHtml.=  " onClick=\"".$this->foot_onclick[ $this->name ]."\" ";
						
						$this->outHtml.=  ">\n";
						$this->outHtml.=  $this->foot_label[ $this->name ];
						break;
						
					default :
						$this->outHtml.=  $this->foot_label[$this->name];
				}
				$this->outHtml.=  "</td>\n";
			}
			$this->outHtml.=  "</TR>\n";
			$this->outHtml.=  "</tfoot>\n";
		}
		$this->outHtml.=  "</TABLE>\n";
/*
------------------------------------------------------------------------------------------------------------
	Campi nascosti per passaggio paramentri per refresh
------------------------------------------------------------------------------------------------------------
*/
		$this->outHtml.=  "<INPUT type=\"hidden\" id=\"".$this->grid_name."_curr_page\" name=\"".$this->grid_name."_curr_page\" value=\"".$this->curr_page."\">\n";
		$this->outHtml.=  "<INPUT type=\"hidden\" id=\"".$this->grid_name."_pages\" name=\"".$this->grid_name."_pages\" value=\"".$this->pages."\">\n";
		$this->outHtml.=  "<INPUT type=\"hidden\" id=\"".$this->grid_name."_orderby\" name=\"".$this->grid_name."_orderby\" value=\"".$this->orderby."\">\n";
/*
------------------------------------------------------------------------------------------------------------
	Programma javascript per spostamento pagine
------------------------------------------------------------------------------------------------------------
*/
		$this->outHtml.=  "<SCRIPT language=\"JavaScript\">\n";
		$this->outHtml.=  "	function ".$this->grid_name."_page_up(p) { document.getElementById(\"".$this->grid_name."_curr_page\").value = parseInt(document.getElementById(\"".$this->grid_name."_curr_page\").value) + p; document.".$this->form.".submit(); }\n";
		$this->outHtml.=  "	function ".$this->grid_name."_page_down(p) { document.getElementById(\"".$this->grid_name."_curr_page\").value = parseInt(document.getElementById(\"".$this->grid_name."_curr_page\").value) - p; document.".$this->form.".submit(); }\n";
		$this->outHtml.=  "	function ".$this->grid_name."_order(p) { \n";
		$this->outHtml.=  "		if (parseInt(document.getElementById(\"".$this->grid_name."_orderby\").value) == p) {\n";
		$this->outHtml.=  "			document.getElementById(\"".$this->grid_name."_orderby\").value = parseInt(document.getElementById(\"".$this->grid_name."_orderby\").value) * -1;\n";
		$this->outHtml.=  "		} else {\n";
		$this->outHtml.=  "			document.getElementById(\"".$this->grid_name."_orderby\").value = p;\n";
		$this->outHtml.=  "		}\n";
		$this->outHtml.=  "		document.".$this->form.".submit(); \n";
		$this->outHtml.=  "	}\n";
		$this->outHtml.=  "</SCRIPT>\n";

		return;
	}

	/* Calcolo risoluzione in px dalla percentuale data e la risoluzione prestabilita*/
	function ris_x($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->grid_width * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function ris_y($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->grid_height * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	/* Calcolo posizione in px data dalla somma percentuale complessiva degli oggetti 
		che precedono l'attuale
	*/
	function pos_x($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->grid_width * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function pos_y($perc,$diff=0,$def="px") {
		$calc_tmp=ceil($this->grid_height * $perc / 100) + $diff;
		return $calc_tmp.$def;
	}
	
	function button( &$res , $name ) 
	{
		$this->outHtml.=  "<INPUT type=\"button\" ";
		if ($this->col_style_link[$name]!="") $this->outHtml.=  "class=\"".$this->col_style_link[$name]."\" ";
		if ($this->col_onclick[$name]!="")  
		{
			$this->tstr = $this->col_onclick[$name];
			
			if (eregi("%subquery%",$this->tstr)) 
			{
				$this->tstr = str_replace("%subquery%",$res->fields[0],$this->tstr);
			} else 
			{
				foreach($this->col_name as $this->tkey => $this->tval) 
				{
					if (eregi("%".$this->tval."%",$this->tstr)) 
					{
						$this->tstr=str_replace("%".$this->tval."%",$res->fields[$this->col_pos[$this->tval]],$this->tstr);
					}
				}
			}
			$this->outHtml.=  "onclick=\"".$this->tstr."\" ";
		}
		
		if ( $this->col_label[$this->name] != '' ) 
		{
			$this->outHtml.=  "value=\"".$this->col_label[$this->name]."\"";
		} 
		else 
		{
			$this->outHtml.=  "value=\"".$res->fields[$this->col_pos[$name]]."\"";
		}
		$this->outHtml.=  ">\n";
	}
	
	function searchVars( &$res, $tstr )
	{
		foreach( $this->col_name as $tkey => $tval ) 
		{
			if ( eregi("%".$tval."%",$tstr) ) 
			{
				return str_replace( "%".$tval."%", $res->fields[ $tkey ] , $tstr );
			}
		}
	}
	
	function hbutton($name,$pos) {
		$this->outHtml.=  "<INPUT type=\"button\" ";
		$this->outHtml.=  "onclick=\"".$this->grid_name."_order(".$pos.")\" ";
		$this->outHtml.=  "value=\"".$name."\"";
		$this->outHtml.=  ">\n";
	}

	function href(&$res,$name) {
		$this->outHtml.=  "<A href=\"#\" ";
		if ($this->col_style_link[$name]!="") $this->outHtml.=  "class=\"".$this->col_style_link[$name]."\" ";
		if ($this->col_onclick[$name]!="")  {
			$this->tstr=$this->col_onclick[$name];
			if (eregi("%subquery%",$this->tstr)) {
				$this->tstr=str_replace("%subquery%",$res->fields[0],$this->tstr);
			} else {
				foreach($this->col_name as $this->tkey => $this->tval) {
					if (eregi("%".$this->tval."%",$this->tstr)) {
						$this->tstr=str_replace("%".$this->tval."%",$res->fields[$this->col_pos[$this->tval]],$this->tstr);
					}
				}
			}
			$this->outHtml.=  "onclick=\"".$this->tstr."\" ";
		}
		$this->outHtml.=  ">\n";
		if ($this->col_image[$name]=="") {
			$this->outHtml.=  $res->fields[$this->col_pos[$name]];
		} else {
			$this->outHtml.=  "<IMG src=\"".$this->path_images."/".$this->col_image[$name]."\" width=\"".$this->ris_x($this->col_img_width[$name])."\" height=\"".$this->ris_y($this->col_img_height[$name])."\">\n";
		}
		$this->outHtml.=  "</A>\n";
	}

	function hhref($name,$pos) {
		$this->outHtml.=  "<A HREF=\"\" ";
		$this->outHtml.=  "onclick=\"".$this->grid_name."_order('".$pos."')\">\n";
		$this->outHtml.=  $name."\n";
		$this->outHtml.=  "</A>\n";
	}
	
	function find_field(&$res,&$tstr) {
		foreach($this->col_name as $this->fkey => $this->fval) {
			if (eregi("%".$this->fval."%",$tstr)) {
				$tstr = str_replace("%".$this->fval."%",$res->fields[$this->col_pos[$this->fval]],$tstr);
			}
		}
	}

	function format(&$value,$name) 
	{
		if (!isset($this->col_format[$name])) return $value;

		switch($this->col_format[$name]) {
			case "currency":
				setlocale(LC_MONETARY, 'it_IT');
 				return money_format('&#8364; %(!2n', $value);
				break;
			default:
				return $value;
		}
	}
}
?>
